package controller;

//导入dao
import model.Student;
import service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class StudentController{

    private StudentService studentService;

    public void setStudentService(StudentService studentService){
        this.studentService=studentService;
    }

    @RequestMapping("/getAll_student")
    public ModelAndView getAll() throws Exception{
        ArrayList<Student> stu=studentService.getAllStudents();
        ModelAndView modelAndView=new ModelAndView("show_student");
        modelAndView.addObject("students",stu);
        return modelAndView;
    }

    @RequestMapping("/delete_student")
    public String delete(HttpServletRequest request, HttpServletResponse response,
                         RedirectAttributes redirectAttributes) throws Exception{
        String sno= request.getParameter("sno");
        boolean success=studentService.deleteStudent(sno);
        if(success){
            redirectAttributes.addFlashAttribute("succ", "删除成功！");
            return "redirect:/show_student.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "删除失败！");
            return "redirect:/show_student.jsp";
        }

    }

    @RequestMapping("/get_student")
    public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String sno= request.getParameter("sno");
        String classId= request.getParameter("classId");
        ModelAndView modelAndView=new ModelAndView("show_student1");
        ArrayList<Student> stu;
        if(sno!=null){    //按学号查询
            stu=studentService.getStudent_sno(sno);

        } else{    //按班级查询
            stu=studentService.getStudent_id(classId);

        }
        modelAndView.addObject("stu",stu);
        return modelAndView;
    }

    @RequestMapping("/edit_student")
    public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String sno= request.getParameter("sno");
        ArrayList<Student> stu=studentService.getStudent_sno(sno);
        ModelAndView modelAndView=new ModelAndView("show_student");
        modelAndView.addObject("student",stu);
        return modelAndView;
    }

    @RequestMapping("/doEdit_student")
    public String doEdit(HttpServletRequest request,HttpServletResponse response,
                         String sPassword,String sName,String sSex,String classId,String IDCard,
                         String address,String birthday,RedirectAttributes redirectAttributes) throws Exception{
        String sno= request.getParameter("sno");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date nbirth=new Date();
        try{
            java.util.Date date=sdf.parse(birthday);
            nbirth=date;
        }catch (ParseException e){
            e.printStackTrace();
        }
        Student student=new Student();
        student.setSno(sno);
        student.setsPassword(sPassword);
        student.setsName(sName);
        student.setsSex(sSex);
        student.setClassId(classId);
        student.setIDCard(IDCard);
        student.setAddress(address);
        student.setBirthday(nbirth);
        boolean succ=studentService.editStudent(student);
        if(succ){
            redirectAttributes.addFlashAttribute("succ", "修改成功！");
            return "redirect:/show_student.jsp";
        }else {
            redirectAttributes.addFlashAttribute("error", "修改失败！");
            return "redirect:/show_student.jsp";
        }

    }

    @RequestMapping("/add_student")
    public ModelAndView add() throws Exception{
        ModelAndView modelAndView=new ModelAndView("show_student");
        return modelAndView;

    }

    @RequestMapping("/doAdd_student")
    public String doAdd(String sno,String sPassword,String sName,String sSex,String classId,
                        String IDCard,String address,String birthday,RedirectAttributes redirectAttributes)throws Exception{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date nbirth=new Date();
        try{
            java.util.Date date=sdf.parse(birthday);
            nbirth=date;
        }catch (ParseException e){
            e.printStackTrace();
        }
        Student student=new Student();
        student.setSno(sno);
        student.setsPassword(sPassword);
        student.setsName(sName);
        student.setsSex(sSex);
        student.setClassId(classId);
        student.setIDCard(IDCard);
        student.setAddress(address);
        student.setBirthday(nbirth);
        boolean succ=studentService.addStudent(student);
        if (succ){
            redirectAttributes.addFlashAttribute("succ", "添加成功！");
            return "redirect:/show_student.jsp";
        }else {
            redirectAttributes.addFlashAttribute("succ", "添加失败！");
            return "redirect:/show_student.jsp";
        }
    }

}